package com.example.service.impl;


import com.example.mapper.suspectProductListMapp;
import com.example.pojo.queryResult;
import com.example.pojo.suspectProductListPojo;
import com.example.service.suspectProductListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.function.support.RouterFunctionMapping;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class suspectProductListImpl implements suspectProductListService {
    @Autowired
    private suspectProductListMapp suspectProductListMapp;
    @Autowired
    private RouterFunctionMapping routerFunctionMapping;

    //新建一个新的数据，并插入数据库中。
    public void insertOne(suspectProductListPojo pojo) {
        pojo.setCreate_time(LocalDateTime.now());
        pojo.setUpdate_time(LocalDateTime.now());
        UUID uuid = UUID.randomUUID();
        String uuidStr = uuid.toString().replace("-", "");
        pojo.setId(uuidStr);
        suspectProductListMapp.insertOne(pojo);
    }

    //在可疑品数据表中查询所有的数据
    public List<suspectProductListPojo> queryAll() {
        List<suspectProductListPojo> pojoList = suspectProductListMapp.queryAll();
        return pojoList;
    }

    //多条件筛选可疑品清单
    public queryResult filterSuspectProduct(Map<String, Object> map){
        queryResult queryResult = new queryResult();
        Integer page =(Integer) map.get("page");
        Integer size=(Integer) map.get("size");
        String link=(String) map.get("link");
        String occurPerson=(String) map.get("occurPerson");
        String startTime=(String) map.get("startTime");
        String endTime=(String) map.get("endTime");
        String status1=(String) map.get("status1");
        Integer startRow=(page-1)*size;
        //查询数据
        List<suspectProductListPojo> pojos=suspectProductListMapp.filterSuspectProduct(startRow, size, link, occurPerson, startTime, endTime, status1);
        queryResult.setData(pojos);
        //查询条数
        Integer total = suspectProductListMapp.filterSuspectProductQuantity(link, occurPerson, startTime, endTime, status1);
        queryResult.setTotal(total);
        return queryResult;
    }

    //修改一条数据，更新到数据库中
    public void editOne(suspectProductListPojo pojo) {
        pojo.setUpdate_time(LocalDateTime.now());
        suspectProductListMapp.editOne(pojo);
    }

    //修改一条数据,根据筛选的字段名和字段值进行筛选数据并返回
    public List<suspectProductListPojo> queryOneField(Map<String, Object> map) {
        String fieldName = (String) map.get("fieldName");
        String fieldVal = (String) map.get("fieldVal");
        List<suspectProductListPojo> pojo = suspectProductListMapp.queryOneField(fieldName, fieldVal);
        return pojo;
    }

    //修改一条数据,用于可疑品处置的字段，根据传递的ID、字段名称和字段值，进行自动匹配数据库中的内容（利用map功能）
    public void editOneFiledById(Map<String, Object> map) {
        String fieldName = (String) map.get("filedName");
        String fieldVal = (String) map.get("filedVal");
        String id = (String) map.get("id");
        suspectProductListMapp.editOneFiledById(id, fieldName, fieldVal);
    }
    public void editTwoFiledsById(Map<String, Object> map) {
        String fieldName1 = (String) map.get("filedName1");
        String fieldVal1 = (String) map.get("filedVal1");
        String fieldName2 = (String) map.get("filedName2");
        String fieldVal2 = (String) map.get("filedVal2");
        String id = (String) map.get("id");
        suspectProductListMapp.editTwoFiledsById(id, fieldName1, fieldVal1, fieldName2, fieldVal2);
    }

    //查询一条数据,根据筛选的日期值进行筛选数据并返回（利用map功能）
    public List<suspectProductListPojo> queryByTimeScope(Map<String, Object> map) {
        String fieldName = (String) map.get("fieldName");
        String minVal = (String) map.get("minVal");
        String maxVal = (String) map.get("maxVal");
        // 定义日期时间格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 将字符串转换为 LocalDateTime
        LocalDateTime minDateTime = LocalDateTime.parse(minVal, formatter);
        LocalDateTime maxDateTime = LocalDateTime.parse(maxVal, formatter);
        //传递参数，调用mapp层执行SQL查询
        List<suspectProductListPojo> pojos = suspectProductListMapp.queryByTimeScope(fieldName,minDateTime,maxDateTime);

        return pojos;
    }
}
